﻿using System;
using Resources;
using Service;
using Model;


public partial class EquipmentModify : System.Web.UI.Page
{
    Equipment _Equipment = new Equipment();
    readonly StockService _StockService  = new StockService();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            
            int equipmentId = GetEquipmentId();
            pnlBody.Visible = equipmentId == 0 ? false : true;
            if (equipmentId == 0) return;

            if (equipmentId != 0) DisplayDetailInfo(equipmentId);
            else ClearPurchaseInfo();
            string codeNumber = _Equipment != null ? _Equipment.CodeNumber : string.Empty;
            lblCodeNumber.Text = Resource.LabelEquipmentModify + " " + codeNumber;
          //  SetDisplayModeToComponents();

        }
    }
    private void PermissionNotAllowed()
    {
        int equipmentId = GetEquipmentId();
        if (equipmentId != 0) DisplayDetailInfo(equipmentId);
        else ClearPurchaseInfo();
        string codeNumber = _Equipment != null ? _Equipment.CodeNumber : string.Empty;
        lblCodeNumber.Text = Resource.LabelEquipmentModify + " " + codeNumber;
        SetDisplayModeToComponents();
    }
    private void SetDisplayModeToComponents()
    {
        bool flagEnable = !String.IsNullOrEmpty(Request.QueryString["displayMode"])
                              ? Request.QueryString["displayMode"].Equals("true") ? false : true
                              :
                                  false;
        btnValidate.Visible = flagEnable;
        
    }
   
    public int GetEquipmentId1()
    {
        return !String.IsNullOrEmpty(Request.QueryString["equipmentId"])? Int32.Parse(Request.QueryString["equipmentId"]) : 0;
    }

    public int GetEquipmentId()
    {
        if (Session["equipmentId"] != null) return Int32.Parse(Session["equipmentId"].ToString());
        return 0;
    }


    public int GetModelId()
    {
        return _Equipment != null ? _Equipment.ModelId : 0;
    }
    
    private void DisplayDetailInfo(int equipmentId)
    {
        try
        {
            
             _Equipment = _StockService.GetEquipment(equipmentId);
            if (_Equipment.PurchaseInfoEquip != null)
            {
                chkIsWarranty.Checked = _Equipment.PurchaseInfoEquip.IsWarranty;
                txtEndDate.Text =
                    UitilityService.ConvertDateToString(_Equipment.PurchaseInfoEquip.EndDate, Resource.DateEnglishFormat);
                txtStartDate.Text = _Equipment.PurchaseInfoEquip.StartDate != null ?
                    _Equipment.PurchaseInfoEquip.StartDate.ToString(Resource.DateEnglishFormat)
                    :string.Empty;

                txtEndDate.Text = _Equipment.PurchaseInfoEquip.EndDate != null ?
                   _Equipment.PurchaseInfoEquip.EndDate.ToString(Resource.DateEnglishFormat)
                :string.Empty;

                txtContractDescription.Text = _Equipment.PurchaseInfoEquip.ContractDescription;

                 //purchase info
                txtPONumber.Text = _Equipment.PurchaseInfoEquip.POReferenceNumber;
                txtPurchaseDate.Text = _Equipment.PurchaseInfoEquip.PurchaseDate != null
                                                   ?
                                                       _Equipment.PurchaseInfoEquip.PurchaseDate.ToString(
                                                           Resource.DateEnglishFormat)
                                                   : string.Empty;
                    ;
                txtUnitPrice.Text = _Equipment.PurchaseInfoEquip.UnitPrice != 0
                                        ?
                                            _Equipment.PurchaseInfoEquip.UnitPrice.ToString("N2")
                                        : string.Empty;
                
                txtPurchaseDescription.Text = _Equipment.PurchaseInfoEquip.PurchaseDescription;

                drlSupplier.SelectedValue =
                    _Equipment.PurchaseInfoEquip.CompanyId.ToString()
                    ;
                drlModel.SelectedValue =
                    _Equipment.ModelId.ToString()
                    ;

            }
            else
            {
                ClearPurchaseInfo();
            }
        }
        catch (Exception ex)
        {
           
        }
    }
    private void ClearPurchaseInfo()
    {
        //warranty info
        chkIsWarranty.Checked = false;
        txtEndDate.Text = string.Empty;
        txtStartDate.Text = string.Empty;
        txtContractDescription.Text = string.Empty;

         //purchase info
        txtPONumber.Text = string.Empty;
        txtPurchaseDate.Text = string.Empty;
        
        txtUnitPrice.Text = string.Empty;
        txtPurchaseDescription.Text = string.Empty;
    }

    
    

    protected void btnValidate_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionEditEquipmentPurchaseInfo))
        {
            lblMessageValidate.Text = Resource.Permission ;
            PermissionNotAllowed();
            return;
        }

        try
        {

            if (!ValidateData()) return;

            //get model
            int equipmentId = GetEquipmentId();
           // if (Request.QueryString["equipmentId"] != null)
            if (equipmentId != 0)
                _Equipment = _StockService.GetEquipment(equipmentId);
               // _Equipment = _StockService.GetEquipment(Int32.Parse(Request.QueryString["equipmentId"])); Before use session

            //get the purchase information 
            EquipPurchaseInfo equipPurchaseInfo = GetEquipPurchaseInfo(_Equipment);
            _StockService.UpdatePurchaseInfo(equipPurchaseInfo);
            lblMessageValidate.Text = Resource.MessageValidate; 



            /*  1. if modify 
               owner.DateEnter
             *  2. Duplicate 
             *  ==> a. auto generate the code  number : list three number available
             *      b. manually enter code number
            */

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private bool ValidateData()
    {
        if(chkIsWarranty.Checked)
        {
            if (String.IsNullOrEmpty(txtStartDate.Text))
            {
                lblRequiredStartDate.Text = Resource.MsgFillInformation;
                return false;
            }
            lblRequiredStartDate.Text = string.Empty;
            if (String.IsNullOrEmpty(txtEndDate.Text))
            {
                lblRequiredEndDate.Text = Resource.MsgFillInformation;
                return false;
            }
            lblRequiredEndDate.Text = string.Empty;
            return true;
        }

        return true;
   }

    private EquipPurchaseInfo GetEquipPurchaseInfo(Equipment equip)
    {
        EquipPurchaseInfo equipmentPurchaseInfo = equip.PurchaseInfoEquip;
        equipmentPurchaseInfo.CompanyId = Int32.Parse(drlSupplier.SelectedValue);
        equipmentPurchaseInfo.ContractDescription = txtContractDescription.Text;
        equipmentPurchaseInfo.IsWarranty = chkIsWarranty.Checked;
        equipmentPurchaseInfo.POReferenceNumber = txtPONumber.Text;
        equipmentPurchaseInfo.PurchaseDate = new Utility().ConvertToCustomizedFormat(txtPurchaseDate.Text);
            
        equipmentPurchaseInfo.PurchaseDescription = txtPurchaseDescription.Text;

        equipmentPurchaseInfo.StartDate = new Utility().ConvertToCustomizedFormat(txtStartDate.Text);


        equipmentPurchaseInfo.EndDate = new Utility().ConvertToCustomizedFormat(txtEndDate.Text);
            

        equipmentPurchaseInfo.UnitPrice = !String.IsNullOrEmpty(txtUnitPrice.Text) ? float.Parse(txtUnitPrice.Text) : 0;
        
        return equipmentPurchaseInfo;

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("EquipmentDisplay.aspx?pageIndex=" + Request.QueryString["pageIndex"]);
    }
    

    
}
